const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    users: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var interval = setInterval(function () {
      if (that.data.users.id) {
        clearInterval(interval);
      } else {
        that.setData({
          users: wx.getStorageSync('USERS')
        });
      }
    }, 1 * 10);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    });
  },
  formSubmit: function (e) {
    var that = this;
    var parmas = {
      "uid": that.data.users.id,
      "email": e.detail.value.email,
      "mobile": e.detail.value.mobile,
      "realname": e.detail.value.realname
    }
    if (parmas.uid && parmas.uid > 0 && parmas.realname && parmas.realname != "") {
      util.wxPost({
        url: "/rest/lufei/nuser", params: parmas,
        success: function (res) {
          util.hideToast();
          if (res.data.status == "000") {
            var users = that.data.users;
            users.realname = parmas.realname;
            users.mobile = parmas.mobile;
            users.email = parmas.email;
            wx.setStorage({
              key: "USERS",
              data: users,
              success: function () {
                wx.showToast({
                  title: "保存成功",
                  icon: "success",
                  duration: 3000,
                  success: function () {
                    wx.switchTab({
                      url: '/pages/users/users'
                    });
                  }
                });
              }
            });
          } else {
            wx.showToast({
              title: "保存失败!",
              icon: "failure",
              duration: 1500
            });
          }
        }
      });
    } else {
      wx.showToast({
        title: "真实姓名必填!",
        icon: "none",
        duration: 1500
      });
    }
  }
})